package com.srwl.mytx.adapter;

import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bm.library.Info;
import com.bm.library.PhotoView;
import com.srwl.mytx.R;
import com.srwl.mytx.domain.MyFile;
import com.srwl.mytx.utils.GlideLoadUtils;
import com.srwl.mytx.utils.Utils;

import java.util.List;


public class PickPictureAdapter extends RecyclerView.Adapter<PickPictureAdapter.ViewHolder> {

    private Context context;

    private List<MyFile> itemList;
    private final static String TAG = "PickPictureAdapter";
    private InteractionListener mOnItemClickListener;

    public PickPictureAdapter(Context context, List<MyFile> result) {
        this.context = context;
        this.itemList = result;
    }


    public PickPictureAdapter(Context context, List<MyFile> itemList, InteractionListener mOnItemClickListener) {
        this.context = context;
        this.itemList = itemList;
        this.mOnItemClickListener = mOnItemClickListener;
    }


    public void setOnItemClickListener(InteractionListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.tx_publish_topic_item_layout, null));
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        MyFile item = itemList.get(position);
        switch (item.getType()) {
            case MyFile.FILE_TYPE_SELECT_REMOTE_FILE:
                holder.tv_ItemType.setText("文件仓库");
                holder.iv_add.setVisibility(View.VISIBLE);
                holder.iv_delete.setVisibility(View.INVISIBLE);
                holder.image.setVisibility(View.INVISIBLE);
                holder.iv_playVideo.setVisibility(View.INVISIBLE);
                holder.itemView.setOnClickListener(v -> mOnItemClickListener.onSelectRemoteFile());
                break;
            case MyFile.FILE_TYPE_ADD_VIDEO:
                holder.tv_ItemType.setText("新视频");
                holder.iv_add.setVisibility(View.VISIBLE);
                holder.iv_delete.setVisibility(View.INVISIBLE);
                holder.image.setVisibility(View.INVISIBLE);
                holder.iv_playVideo.setVisibility(View.INVISIBLE);
                holder.itemView.setOnClickListener(v -> mOnItemClickListener.onCreateVideo(holder.itemView, position));
                break;
            case MyFile.FILE_TYPE_ADD_PICTURE:
                holder.tv_ItemType.setText("新图片");
                holder.iv_add.setVisibility(View.VISIBLE);
                holder.iv_delete.setVisibility(View.INVISIBLE);
                holder.image.setVisibility(View.INVISIBLE);
                holder.iv_playVideo.setVisibility(View.INVISIBLE);
                holder.itemView.setOnClickListener(v -> mOnItemClickListener.onPickNewPicture(position));
                break;
            case MyFile.FILE_TYPE_VIDEO:
                holder.iv_add.setVisibility(View.INVISIBLE);
                holder.image.setVisibility(View.VISIBLE);
                holder.iv_playVideo.setVisibility(View.VISIBLE);
                holder.iv_delete.setVisibility(View.VISIBLE);
                if (item.getStatus() == MyFile.FILE_STATUS_REMOTE) {
                    holder.iv_delete.setImageResource(R.drawable.tx_delete_circle_blue);
                    //文件上传完成后，url 里面只是 文件的key(远程文件名字)，并不是完整的url
                    // 完整的类型这样  https://im-a.oss-cn-beijing.aliyuncs.com/201906150168150888
                    String remoteUrl = Utils.getIconUrl(item);

                    Log.i(TAG, "onBindViewHolder: 远程视频封面地址：" + remoteUrl);
//                    String url = item.getVideoCover();//视频类型优先展示封面图
//                    if (!TextUtils.isEmpty(url)) {
//                        remoteUrl = AliOssService.TOPIC_IMAGE_PRE_URL + url;
//                    } else {
//                        url = item.getUrl();
//                        if (url.startsWith("http")) {
//                            remoteUrl = url;
//                        } else {
//                            remoteUrl = AliOssService.TOPIC_VIDEO_PRE_URL + url;
//                        }
//                    }
                    showCover(remoteUrl, holder.image);
                } else {
                    holder.iv_delete.setImageResource(R.drawable.tx_delete_circle_red);
                    String localPath = item.getVideoCover();
                    if (TextUtils.isEmpty(localPath)) {
                        localPath = item.getUrl();
                    }
                    showCover(localPath, holder.image);
                }
                holder.iv_delete.setOnClickListener(v -> mOnItemClickListener.onVideoRemove(position, item));
                holder.itemView.setOnClickListener(v -> mOnItemClickListener.onShowBigImage(item, PhotoView.getBigImageViewInfo(holder.image)));
                break;

            case MyFile.FILE_TYPE_PICTURE:
                holder.iv_playVideo.setVisibility(View.INVISIBLE);
                holder.iv_add.setVisibility(View.INVISIBLE);
                holder.image.setVisibility(View.VISIBLE);
                holder.iv_delete.setVisibility(View.VISIBLE);
                if (item.getStatus() == MyFile.FILE_STATUS_REMOTE) {
//                    String url = item.getUrl();
//                    String remoteUrl;
//                    if (url != null && url.startsWith("http")) {
//                        remoteUrl = url;
//                    } else {
//                        remoteUrl = AliOssService.TOPIC_IMAGE_PRE_URL + url;
//                    }

                    String remoteUrl = Utils.getIconUrlScale50(item);

                    showCover(remoteUrl, holder.image);
                    holder.iv_delete.setImageResource(R.drawable.tx_delete_circle_blue);
                } else {
                    // RequestOptions options = new RequestOptions().placeholder(R.drawable.ease_default_image).error(R.drawable.tx_default_place);
                    //Glide.with(context).load(item.getUrl()).transition(new DrawableTransitionOptions().crossFade()).into(holder.image);
                    showCover(item.getUri(), holder.image);
                    holder.iv_delete.setImageResource(R.drawable.tx_delete_circle_red);
                }
                holder.iv_delete.setOnClickListener(v -> mOnItemClickListener.onPictureRemove(position, item));
                holder.itemView.setOnClickListener(v -> mOnItemClickListener.onShowBigImage(item, PhotoView.getBigImageViewInfo(holder.image)));

        }

    }

    private void showCover(String remoteUrl, ImageView imageView) {
        // Glide.with(context).load(remoteUrl).diskCacheStrategy(DiskCacheStrategy.ALL).into(imageView);
        GlideLoadUtils.glideLoad(context, remoteUrl, imageView);
    }

    private void showCover(Uri localUri, ImageView imageView) {
        //Glide.with(context).load(localUri).diskCacheStrategy(DiskCacheStrategy.ALL).into(imageView);
        GlideLoadUtils.glideLoad(context, localUri, imageView);

    }

    @Override
    public int getItemCount() {
        return itemList.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {

        public ImageView image;
        ImageView iv_delete;
        TextView tv_ItemType;
        ImageView iv_playVideo;
        ImageView iv_add;

        public ViewHolder(View itemView) {
            super(itemView);
            image = itemView.findViewById(R.id.iv_picture);
            iv_delete = itemView.findViewById(R.id.iv_delete);
            tv_ItemType = itemView.findViewById(R.id.tv_item_type);
            iv_playVideo = itemView.findViewById(R.id.iv_play_video);
            iv_add = itemView.findViewById(R.id.iv_add);
        }

    }


    public interface InteractionListener {

        void onSelectRemoteFile();

        void onPickNewPicture(int position);

        void onShowBigImage(MyFile targetPictureFile, Info info);

        void onCreateVideo(View view, int position);

        @Deprecated
        void onShowVideo(MyFile targetVideoFile, int position);

        void onPictureRemove(int position, MyFile targetPictureFile);

        void onVideoRemove(int position, MyFile targetVideoFile);
    }

}
